package com.antifly.common.exception;


import com.antifly.common.constant.ResponseCode;
import com.antifly.common.exception.bizException.BizException;
import com.antifly.common.constant.ResponseBean;
import feign.FeignException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;
import java.util.stream.Collectors;

@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理自定义异常
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ResponseBean bizExceptionHandler(BizException e) {
        return new ResponseBean(e.getCode(), e.getMessage());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseBean exceptionHandler(Exception e) {
        e.printStackTrace();
        ResponseBean responseBean = new ResponseBean(e);
        responseBean.setCode("-1");
        responseBean.setMsg(e.getMessage());
        return responseBean;
    }

    /**
     * 参数解析异常
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseBean handleValidationBodyException(MethodArgumentNotValidException exception) {
        exception.printStackTrace();
        ResponseBean responseBean = new ResponseBean(ResponseCode.NOT_VALID_ERROR);
        responseBean.setData(Objects.requireNonNull(exception.getBindingResult().getFieldError()).getDefaultMessage());
        return responseBean;
    }

    /**
     * 校验错误拦截处理
     *
     * @param exception 错误信息集合
     * @return 错误信息
     */
    @ExceptionHandler(BindException.class)
    public ResponseBean validationBodyException(BindException exception) {
        exception.printStackTrace();
        String collect = exception.getBindingResult().getAllErrors().stream()
                .map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(";"));
        ResponseBean responseBean = new ResponseBean(ResponseCode.NOT_VALID_ERROR);
        responseBean.setData(collect);
        return responseBean;
    }

    /**
     * 参数格式校验
     *
     * @param exception 错误信息集合
     * @return 错误信息
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseBean httpMessageNotReadableExceptionException(HttpMessageNotReadableException exception) {
        exception.printStackTrace();
        ResponseBean responseBean = new ResponseBean(ResponseCode.PARAM_VALIDATE_ERROR);
        responseBean.setData(exception.getMessage());
        return responseBean;
    }


    /**
     * 参数格式校验
     *
     * @param exception 错误信息集合
     * @return 错误信息
     */
    @ExceptionHandler(FeignException.class)
    public ResponseBean feignException(FeignException exception) {
        exception.printStackTrace();
        ResponseBean responseBean = new ResponseBean(ResponseCode.FEIGN_CALL_FAILED);
        responseBean.setData(exception.getMessage());
        return responseBean;
    }

    /**
     * 接口缺少参数错误
     *
     * @param exception 错误信息集合
     * @return 错误信息
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResponseBean feignException(MissingServletRequestParameterException exception) {
        exception.printStackTrace();
        ResponseBean responseBean = new ResponseBean(ResponseCode.MISS_PARAMETER_ERROR);
        responseBean.setData(exception.getMessage());
        return responseBean;
    }

    /**
     * 请求方式错误
     *
     * @param exception 错误信息集合
     * @return 错误信息
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseBean httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
        exception.printStackTrace();
        ResponseBean responseBean = new ResponseBean(ResponseCode.HTTP_REQUEST_METHOD_NOT_SUPPORTED_EXCEPTION);
        responseBean.setData(exception.getMessage());
        return responseBean;
    }
}
